package foodmaterial.config;

import foodmaterial.entity.SysUser;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Aspect
@Order(-1)
@Component
public class DynamicDataSourceAspect {
    @Before("@annotation(foodmaterial.config.ChoiceDataSource))")
    public void before() {
        SysUser userDetails = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        DataSourceContextHolder.setDBType(userDetails.getDb());
    }

    @After("@annotation(foodmaterial.config.ChoiceDataSource))")
    public void after() {
        DataSourceContextHolder.clearDBType();
    }
}
